import { PrismaClient } from '@prisma/client';
import {
  Body,
  Controller,
  Get,
  Param,
  ParseIntPipe,
  Post,
  UsePipes,
} from '@nestjs/common';
import { AppService } from './app.service';
import { HdPipe } from './hd.pipe';
import CreateArticleDto from './dto/create.article.dto';

@Controller()
export class AppController {
  prisma: PrismaClient;
  constructor(private readonly appService: AppService) {
    this.prisma = new PrismaClient();
  }

  @Get(':id')
  @UsePipes(HdPipe)
  getHello(@Param('id') id: number) {
    return this.prisma.article.findUnique({
      where: {
        id: id,
      },
    });
  }

  @Post('store')
  async add(@Body(HdPipe) dto: CreateArticleDto) {
    const article = await this.prisma.article.create({
      data: {
        title: dto.title,
        content: dto.content,
      },
    });
    return article;
  }
}
